package com.kestrel.space.user.controller;

import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.user.model.User;
import com.kestrel.space.user.query.UserQuery;
import com.kestrel.space.user.service.UserService;
import com.kestrel.space.common.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.kestrel.space.common.utils.Result;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/8/21
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @RequestMapping("register")
    public Result register(@RequestBody User user){
        userService.saveOrUpdateUser(user, "save");
        return Result.ok("用户注册成功");
    }

    @RequestMapping("login")
    public Result login(@RequestBody User user){
        List<String> message = userService.login(user);
        return Result.list(message);
    }

    @RequestMapping("logout")
    public Result logout(){
        return Result.ok("用户：" + "登出成功");
    }

    @RequestMapping("getUser")
    public Result getUser(@RequestParam Long id){
        return Result.ok(userService.getUser(id));
    }

    @RequestMapping("listUsers")
    public Result listUsers(@RequestBody UserQuery query){
        return Result.list(userService.listUsers(query, new LargePage(query.getPage(), query.getRows())));
    }

    /**
     * @param user
     * @param type
     * @return
     */
    @RequestMapping("saveOrUpdateUser")
    public Result saveOrUpdateUser(@RequestBody User user, @RequestParam String type){
        return Result.ok(userService.saveOrUpdateUser(user, type));
    }

    @RequestMapping("batchSaveOrUpdateUsers")
    public Result batchSaveOrUpdateUsers(@RequestBody List<User> userList, @RequestParam String type){
        return Result.ok(userService.batchSaveOrUpdateUsers(userList, type));
    }
}
